package com.bjpowernode.springboot.service.Impl;

import com.bjpowernode.springboot.dao.UserMapper;
import com.bjpowernode.springboot.entity.BookBean;
import com.bjpowernode.springboot.entity.UserBean;
import com.bjpowernode.springboot.service.UserService;
import com.bjpowernode.springboot.utils.UserBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate; // 注入redis模板对象，实现对redis的操作

    @Override
    public List<UserBean> queryAll() {
        return userMapper.queryAll();
    }

    @Override
    public boolean addRegisterUser(UserBean userBean) {
        int i = userMapper.addUser(userBean);
        return i > 0;
    }

    @Override
    public UserBean queryUserByUserInfo(UserBean userBean) {
        return userMapper.queryUserByUserInfo(userBean);
    }

    @Override
    public void cacheUserBeanToRedis(UserBean queryUser) { // hash结构缓存用户的登录信息
        // 创建hash结构
        HashOperations<Object, Object, Object> hashOperations = redisTemplate.opsForHash();
        // 获取bean中的数据
        String userId = queryUser.getUserId() + "";
        String username = queryUser.getUsername();
        String password = queryUser.getPassword();
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("username",username);
        userMap.put("password",password);
        hashOperations.putAll(userId,userMap);
        redisTemplate.expire(userId,15, TimeUnit.SECONDS); // 设置key：userId的过期时间
    }

    @Override
    public boolean isLogined(String username, String password) {
        HashOperations<Object, Object, Object> hashOperations = redisTemplate.opsForHash();
        // 先判断key是否过期
        Set<Object> keySet = redisTemplate.keys("userId");
        for (Object key : keySet) {
            Map<Object, Object> entries = hashOperations.entries(key);
            if (entries.get("username").equals(username) && entries.get("password").equals(password)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public UserBean getUserFromRedis(String userId) {
        UserBean userBean = new UserBean();
        String username = (String) redisTemplate.opsForHash().get(userId, "username");
        String password = (String) redisTemplate.opsForHash().get(userId, "password");
        userBean.setPassword(password);
        userBean.setUsername(username);
        return userBean;
    }

    @Override
    public List<BookBean> queryAllBooks() {
        List<BookBean> bookBeans = userMapper.queryAllBook();
        return bookBeans;
    }
}
